import instance from "../../utils/util"

// pages/cart/cart.js
const computedBehavior = require("miniprogram-computed").behavior;
const app = getApp()
Page({
    //注册计算属性
    behaviors: [computedBehavior],
    computed: {
        selectAllStatus(data) {

            return data.cart.every((item) => item.isChecked === 1)

        },
        totalPrice(data) {
            console.log(data)
            let num = 0;
            data.cart.forEach(el => {
                if (el.isChecked == 1) {
                    num += (el.price * el.count)

                }
            })
            console.log(num)
            return num;
        }
    },
    /**
     * 页面的初始数据
     */
    data: {
        cart: [],
        show: false,
        qx: true,
        totalprice: 0

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getdata()
    },
    async getdata() {
        let data = JSON.parse(wx.getStorageSync('info'));
        const res = await instance.get("/api/shopping-carts?userId=" + data.id);
        console.log(res)

        this.setData({
            cart: res.data
        })
        this.data.cart.forEach(el => {
            el.num = el.quantity
            el.ischeck = false
            el.remark = ""
        })
        this.setData({
            cart: this.data.cart
        })
        this.data.totalprice=0
        this.data.cart.forEach(el => {
            if (el.ischeck) {
                this.data.totalprice += el.product.price * el.num;

            }
        })
        this.setData({
            totalprice: this.data.totalprice
        })
    },
    add(e) {
        console.log(e)
        this.data.cart[e.currentTarget.dataset.index].num++;
        this.setData({
            cart: this.data.cart
        })
        this.data.totalprice = 0;
        this.data.cart.forEach(el => {
            if (el.ischeck) {
                this.data.totalprice += el.product.price * el.num;

            }
        })
        this.setData({
            totalprice: this.data.totalprice
        })
    },
    inputt(e) {
        console.log(e)
        this.data.cart[e.currentTarget.dataset.index].remark = e.detail.value;
        this.setData({
            cart: this.data.cart
        })
    },
    jian(e) {
        console.log(e)
        if (this.data.cart[e.currentTarget.dataset.index].num !== 1) {
            this.data.cart[e.currentTarget.dataset.index].num--;

            this.setData({
                cart: this.data.cart
            })
        }
        this.data.totalprice = 0;
   
        this.data.cart.forEach(el => {
            if (el.ischeck) {
                this.data.totalprice += el.product.price * el.num;

            }
        })
        this.setData({
            totalprice: this.data.totalprice
        })
    },
    onShow() {
        if (wx.getStorageSync('token')) {

            this.getiinfo()
        } else {
            this.setData({
                show: false
            })
        }
    },
    login() {
        wx.navigateTo({
            url: '/pages/login/login',
        })
    },
    shanchu(e) {
        wx.showModal({
            title: '删除',
            content: '是否确认删除该商品？',
            complete: async (res) => {
                if (res.cancel) {

                }

                if (res.confirm) {
                    const res1 = await instance.get(`/cart/delete/${ e.currentTarget.dataset.id}`)
                    if (res1.code == 200) {
                        this.getiinfo()
                    }
                }
            }
        })

    },
    onSubmit() {
        console.log(1213)
        if (this.data.totalPrice === 0) {
            wx.showToast({
                title: '请选择商品',
                icon: "none"
            })
            return
        } else {
            wx.navigateTo({
                url: '/pages/jsdd/jsdd',
            })
        }

    },
async js(){
    let data=JSON.parse( wx.getStorageSync('info'));
    let skulist=[];
    this.data.cart.forEach(el=>{
        if(el.ischeck){
            skulist.push({
                skuId:el.sku.id,
                quantity:el.num,
                receiverName:data.nickname,
                receiverPhone:data.tel,
                receiverProvince: "广东省",
                receiverCity: "深圳市",
                receiverDistrict: "南山区",
                receiverAddress:"科技园路123号", 
                receiverPostcode:518000,
                remark:""
            })
        }
    })
    const res=await instance.post("/api/orders",{
        items:skulist,
        userId:data.id,
        receiverName:data.nickname,
        receiverPhone:data.tel,
        receiverProvince: "广东省",
        receiverCity: "深圳市",
        receiverDistrict: "南山区",
        receiverAddress:"科技园路123号", 
        receiverPostcode:518000,

    })
    console.log(res)
    wx.showToast({
      title: '成功',
    })
},
    onChange1(e) {
        console.log(e)
        this.data.cart[e.currentTarget.dataset.index].ischeck=!this.data.cart[e.currentTarget.dataset.index].ischeck
        this.setData({
            cart: this.data.cart
        })
        this.data.totalprice = 0;
   let isqx=true;
        this.data.cart.forEach(el => {
            console.log(el)
            if (el.ischeck == true) {
                this.data.totalprice +=  el.product.price * el.num;
                console.log(this.data.totalprice,"this.data.totalprice")
            
            }else{
isqx=false;
            }
        })
        console.log(this.data.totalprice,"this.data.totalprice")
        this.setData({
            totalprice: this.data.totalprice
        })
        if(isqx == true){
            this.setData({
                selectAllStatus:true
            })
        }else{
            this.setData({
                selectAllStatus:false
            })
        }
    },
    async onchange(e) {
        console.log(e)
       
        // this.getiinfo()
    },
    async getiinfo() {
        const res = await instance.get("/cart/getCartList");
        console.log(res)
        this.setData({
            show: true,
            cart: res.data
        })
        this.data.cart.forEach(el => {
            el.checked = true
        })
        this.setData({
            cart: this.data.cart
        })
    },

    async selectall(e) {
        this.setData({
            selectAllStatus: !this.data.selectAllStatus
        })
        this.data.cart.forEach(el => {
            el.ischeck = !el.ischeck
        })
        this.setData({
            cart: this.data.cart
        })
        this.data.totalprice = 0;
   
        this.data.cart.forEach(el => {
            if (el.ischeck) {
                this.data.totalprice +=  el.product.price * el.num;

            }
        })
        console.log(this.data.totalprice)
        this.setData({
            totalprice: this.data.totalprice
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }



    /**
     * 生命周期函数--监听页面显示
     */

})